window._CJB = {

    selectedAttr:null,

    _EQUIP:null,

    attrList:[
        {attr:'speed',value:25,cut:10},
        {attr:'beginEnergy',value:25,cut:10},
        {attr:'fly',value:1,cut:10},
        {attr:'MagicRecovery',value:10,cut:10},
        {attr:'maxMagic',value:10000,cut:10},
        {attr:'dropLv',value:5,cut:10},
        {attr:'aqDamage',value:100,cut:10},
        {attr:'hpRecovery',value:10,cut:10},
        {attr:'hit',value:100,cut:10},
        {attr:'miss',value:100,cut:10},
        {attr:'eye',value:1,cut:10},

        {attr:'gedang',value:100,cut:20},
        {attr:'damageReduce',value:100,cut:20},
        {attr:'lianjiSkill',value:1,cut:20},
        {attr:'death',value:10,cut:20},
        {attr:'gushang',value:100000,cut:20},
        {attr:'distance',value:4,cut:20},
        {attr:'scope',value:3,cut:20},
        {attr:'magicPer',value:100,cut:20},
        {attr:'invent',value:1,cut:20},

        {attr:'weifeng',value:1,cut:30},
        {attr:'magicCloth',value:1,cut:30},
        {attr:'clearBody',value:1,cut:30},
        {attr:'statusBody',value:1,cut:30},
        {attr:'xiezhan',value:1,cut:30},
        {attr:'beActive',value:1,cut:30},

        {attr:'goldCloth',value:1,cut:50},
        {attr:'poge',value:1,cut:50}
    ],

    showCJB:function(parentNode,param){
        var uiObj = _GRM.getNodeObj('DIYequipDetails');
        var mainLabel = uiObj.node;

        _CJB._CURRENTLABEL = mainLabel;
        _CJB._CURRENTPARAM = param;

        if(uiObj.isnew){
            _CJB.initButton(mainLabel);
        }
        _CJB._EQUIP=null;
        _CJB.clear();
        parentNode.addChild(mainLabel);
        return mainLabel;
    },

    setEquip:function(equip){

      _CJB._EQUIP=equip;
      _CJB.refresh();

    },

    addAttr:function(attrobj){

      if(_CJB.selectedAttr){
        _CJB.selectedAttr.push(attrobj);
        _CJB.refresh();
      }

    },

    refresh:function(){

      if(_CJB._CURRENTLABEL){
        var mainlabel = _CJB._CURRENTLABEL.getChildByName('mainLabel');
        var baseLabel = mainlabel.getChildByName('base');
        var tipsLabel = mainlabel.getChildByName('tips');
        var priceNode = baseLabel.getChildByName('price');
        var goodsNode = baseLabel.getChildByName('goods');
        var attrListNode = baseLabel.getChildByName('attrList').getChildByName('msg');

        if(_CJB._EQUIP){
          baseLabel.active = true;
          tipsLabel.active = false;

          _CJB.refreshGoods(goodsNode);
          _CJB.refreshAttrList(attrListNode);
          priceNode.getComponent(cc.Label).string = _CJB.getPriceStr();

        }else{
          baseLabel.active = false;
          tipsLabel.active = true;
        }
      }

    },

    refreshGoods:function(goodsNode){
      if(_CJB._EQUIP){
        goodsNode.getChildByName('img').getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_IOF.getGoodsImg(_CJB._EQUIP));
        goodsNode.getChildByName('label').getComponent(cc.Label).string = _CJB._EQUIP.type;
        goodsNode.color = new cc.color(_IOF.getGoodsColor(_CJB._EQUIP));
      }
    },


    getAttrMap:function(){

      var attrMap = {};
      if(_CJB._EQUIP){
        for(var key in _CJB._EQUIP.attr){
          if(!_ROLEDATA._mainAttrConfig[key]){
            attrMap[key]=_CJB._EQUIP.attr[key];
          }
        }
      }

      if(_CJB.selectedAttr&&_CJB.selectedAttr.length>0){
        for(var i=0;i<_CJB.selectedAttr.length;i++){
          var key = _CJB.selectedAttr[i].attr;
          var value = _CJB.selectedAttr[i].value;
          if(attrMap[key]){
            attrMap[key]+=value;
          }else{
            attrMap[key]=value;
          }
        }
      }

      return attrMap;

    },

    refreshAttrList:function(attrListNode){
    
      var attrMap = _CJB.getAttrMap();
      var attrstring = "";
      for(var key in attrMap){
        attrstring+=_DICT[key]+":"+attrMap[key]+"\n";
      }

      attrListNode.getComponent(cc.RichText).string = attrstring;
    },

    getNeed:function(){

      var need=0;

      if(_CJB.selectedAttr&&_CJB.selectedAttr.length>0){
        for(var i=0;i<_CJB.selectedAttr.length;i++){
          need+=_CJB.selectedAttr[i].cut;
        }
      }

      return need;

    },

    getPriceStr:function(){
      var need = _CJB.getNeed();
      var total = _GAMEDATAHELPER.getGoodsNmb(window.gameData.specilItem,'神锻石');
      return "神锻石:"+total+"/"+need;
    },

    initButton:function(mainLabel){
        //var mask = node.getChildByName('mask');
        var mainBtns = mainLabel.getChildByName('mainLabel')
                        .getChildByName('base')
                        .getChildByName('btns');
        var makeBtn = mainBtns.getChildByName('make');
        var attrBtn = mainBtns.getChildByName('attr');
        var clearBtn = mainBtns.getChildByName('clear');

        
        makeBtn.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('制作');
            _CJB.makeCjb();
        }, mainLabel);

        attrBtn.on(cc.Node.EventType.TOUCH_START, function(){
          //console.log('副属性');
          _MASELECT.showSelect(_MAP.node);
        }, mainLabel);

        clearBtn.on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('重置选择');
            _CJB.clear();
        }, mainLabel);

    },

    getDesOfAttr:function(obj){

        return _DICT[obj.attr]+":"+obj.value+"("+obj.cut+")";
    },

    clear:function(){
      _CJB.selectedAttr=[];
      _CJB.refresh();
    },

    makeCjb:function(){

      if(_CJB._EQUIP){
        var need = _CJB.getNeed();
        var total = _GAMEDATAHELPER.getGoodsNmb(window.gameData.specilItem,'神锻石');

        if(need<=0){
          _TIPS.showMessage('未选择副属性');
          return false;
        }

        if(
          total>=need
          &&_GAMEDATAHELPER.reduceSomeItemByName('神锻石',need)
        ){
          
          var attrMap = _CJB.getAttrMap();

          for(var key in attrMap){
            _CJB._EQUIP.attr[key]=attrMap[key];
          }
          _CJB._EQUIP.iscjb = true;

          _CJB._EQUIP=null;
          _CJB.clear();
          _TIPS.showMessage('制作成功');
          return true;

        }else{
          _TIPS.showMessage('神锻石不足无法制作');
          return false;

        }

      }

      _TIPS.showMessage('未选择装备');
      return false;
    }

}